package com.hero.jobweb.service.impl;

import static com.hero.jobweb.config.MessagesManger.getSystemMessagesInt;

import java.util.Date;
import java.util.List;

import com.hero.basicsystem.dao.ext.IUserDAOExt;
import com.hero.basicsystem.entity.User;
import com.hero.jobweb.config.ExceptionKey;
import com.hero.jobweb.config.SystemKey;
import com.hero.jobweb.dao.ITbBasicDAO;
import com.hero.jobweb.dao.ITbCareerObjectiveDAO;
import com.hero.jobweb.dao.ITbEducationDAO;
import com.hero.jobweb.dao.ITbItskillDAO;
import com.hero.jobweb.dao.ITbLanguageDAO;
import com.hero.jobweb.dao.ITbPersonalResumeCategoryDAO;
import com.hero.jobweb.dao.ITbProjectExperienceDAO;
import com.hero.jobweb.dao.ITbResumeDAO;
import com.hero.jobweb.dao.ITbWorkExperienceDAO;
import com.hero.jobweb.dao.ext.ITbUsersDAOExt;
import com.hero.jobweb.entity.TbBasic;
import com.hero.jobweb.entity.TbBasicExample;
import com.hero.jobweb.entity.TbCareerObjective;
import com.hero.jobweb.entity.TbCareerObjectiveExample;
import com.hero.jobweb.entity.TbEducation;
import com.hero.jobweb.entity.TbEducationExample;
import com.hero.jobweb.entity.TbItskill;
import com.hero.jobweb.entity.TbItskillExample;
import com.hero.jobweb.entity.TbLanguage;
import com.hero.jobweb.entity.TbLanguageExample;
import com.hero.jobweb.entity.TbPersonalResumeCategory;
import com.hero.jobweb.entity.TbProjectExperience;
import com.hero.jobweb.entity.TbProjectExperienceExample;
import com.hero.jobweb.entity.TbResume;
import com.hero.jobweb.entity.TbResumeExample;
import com.hero.jobweb.entity.TbUserAdapter;
import com.hero.jobweb.entity.TbWorkExperience;
import com.hero.jobweb.entity.TbWorkExperienceExample;
import com.hero.jobweb.exception.BaseException;
import com.hero.jobweb.service.IResumeService;

/**
 * 2011-06-25
 * 完善了注册简历
 * 
 * @author Volcano
 *
 */
public class ResumeServiceImpl extends BaseService implements IResumeService {
	private ITbResumeDAO resumeDAO;
	private ITbBasicDAO basicDAO;
	private ITbWorkExperienceDAO workExperienceDAO;
	private ITbCareerObjectiveDAO careerObjectiveDAO;
	private ITbEducationDAO educationDAO;
	private ITbItskillDAO itskillDAO;
	private ITbProjectExperienceDAO projectExperienceDAO;
	private ITbLanguageDAO languageDAO;
	private ITbPersonalResumeCategoryDAO personalResumeCategoryDAO;
	private IUserDAOExt userDAO;
	private ITbUsersDAOExt tbUsersDAO;

	public ITbUsersDAOExt getTbUsersDAO() {
		return tbUsersDAO;
	}

	public void setTbUsersDAO(ITbUsersDAOExt tbUsersDAO) {
		this.tbUsersDAO = tbUsersDAO;
	}

	public IUserDAOExt getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAOExt userDAO) {
		this.userDAO = userDAO;
	}

	public ITbPersonalResumeCategoryDAO getPersonalResumeCategoryDAO() {
		return personalResumeCategoryDAO;
	}

	public void setPersonalResumeCategoryDAO(
			ITbPersonalResumeCategoryDAO personalResumeCategoryDAO) {
		this.personalResumeCategoryDAO = personalResumeCategoryDAO;
	}

	public ITbBasicDAO getBasicDAO() {
		return basicDAO;
	}

	public void setBasicDAO(ITbBasicDAO basicDAO) {
		this.basicDAO = basicDAO;
	}

	public ITbWorkExperienceDAO getWorkExperienceDAO() {
		return workExperienceDAO;
	}

	public void setWorkExperienceDAO(ITbWorkExperienceDAO workExperienceDAO) {
		this.workExperienceDAO = workExperienceDAO;
	}

	public ITbCareerObjectiveDAO getCareerObjectiveDAO() {
		return careerObjectiveDAO;
	}

	public void setCareerObjectiveDAO(ITbCareerObjectiveDAO careerObjectiveDAO) {
		this.careerObjectiveDAO = careerObjectiveDAO;
	}

	public ITbEducationDAO getEducationDAO() {
		return educationDAO;
	}

	public void setEducationDAO(ITbEducationDAO educationDAO) {
		this.educationDAO = educationDAO;
	}

	public ITbItskillDAO getItskillDAO() {
		return itskillDAO;
	}

	public void setItskillDAO(ITbItskillDAO itskillDAO) {
		this.itskillDAO = itskillDAO;
	}

	public ITbProjectExperienceDAO getProjectExperienceDAO() {
		return projectExperienceDAO;
	}

	public void setProjectExperienceDAO(
			ITbProjectExperienceDAO projectExperienceDAO) {
		this.projectExperienceDAO = projectExperienceDAO;
	}

	public ITbLanguageDAO getLanguageDAO() {
		return languageDAO;
	}

	public void setLanguageDAO(ITbLanguageDAO languageDAO) {
		this.languageDAO = languageDAO;
	}

	public ITbResumeDAO getResumeDAO() {
		return resumeDAO;
	}

	public void setResumeDAO(ITbResumeDAO resumeDAO) {
		this.resumeDAO = resumeDAO;
	}

	public void saveResume(TbResume resume) {
		// TODO Auto-generated method stub

	}

	public void modifyResume(TbResume resume) {
		// TODO Auto-generated method stub

	}

	public void saveBasic(TbBasic basic) {
		// TODO Auto-generated method stub

	}

	public void modifyBasic(TbBasic basic) {
		// TODO Auto-generated method stub

	}

	public void saveEducation(TbEducation education) {
		// TODO Auto-generated method stub

	}

	public void modifyEducation(TbEducation education) {
		// TODO Auto-generated method stub

	}

	public void saveCareerObjective(TbCareerObjective careerObjective) {
		// TODO Auto-generated method stub

	}

	public void modifyCareerObjective(TbCareerObjective careerObjective) {
		// TODO Auto-generated method stub

	}

	public void saveSelfAppraise(String selfAppraise) {
		// TODO Auto-generated method stub

	}

	public void modifySelfAppraise(String selfAppraise) {
		// TODO Auto-generated method stub

	}

	public void saveWorkExperience(TbWorkExperience workExperience) {
		// TODO Auto-generated method stub

	}

	public void modifyWorkExperience(TbWorkExperience workExperience) {
		// TODO Auto-generated method stub

	}

	public void saveProjectExperience(TbProjectExperience projectExperience) {
		// TODO Auto-generated method stub

	}

	public void modifyProjectExperience(TbProjectExperience projectExperience) {
		// TODO Auto-generated method stub

	}

	public void saveLanguage(TbLanguage language) {
		// TODO Auto-generated method stub

	}

	public void modifyLanguage(TbLanguage language) {
		// TODO Auto-generated method stub

	}

	public void saveItSkill(TbItskill itSkill) {
		// TODO Auto-generated method stub

	}

	public void modifyItSkill(TbItskill itSkill) {
		// TODO Auto-generated method stub

	}

	/**
	 * 查询简历列表
	 */
	public List<TbResume> queryResumeList(TbResume tbResume) {
		TbResumeExample resumeExample = new TbResumeExample();
		TbResumeExample.Criteria resumeCri = resumeExample.createCriteria();
		if (tbResume.getUserId() == null) {
			throw new BaseException(ExceptionKey.USER_ID_IS_NULL);
		}
		resumeCri.andUserIdEqualTo(tbResume.getUserId());
		return this.resumeDAO.selectByExample(resumeExample);
	}

	/**
	 * 查询完整简历
	 */
	public FullResume queryFullResumeByResumeId(TbResume tbResume) {

		Integer resumeId = tbResume.getId();
		if (resumeId == null) {
			throw new BaseException(ExceptionKey.RESUME_NOT_FOUND);// 简历未找到
		}
		FullResume fullResume = new FullResume();

		// 查询简历基础信息
		TbResume outResume = this.resumeDAO.selectByPrimaryKey(resumeId);
		if (outResume == null) {
			throw new BaseException(ExceptionKey.RESUME_NOT_FOUND);// 简历未找到
		}
		fullResume.setTbResume(outResume);

		// 查询基础信息
		TbBasic queryBasic = new TbBasic();
		queryBasic.setUserId(outResume.getUserId());
		queryBasic = queryBasic(queryBasic);
		if (queryBasic == null) {
			queryBasic = new TbBasic();
		}
		fullResume.setTbBasic(queryBasic);

		// 查询求职意向
		TbCareerObjectiveExample careerObjectiveExample = new TbCareerObjectiveExample();
		careerObjectiveExample.createCriteria().andResumeIdEqualTo(resumeId);
		List<TbCareerObjective> careerObjectiveList = this.careerObjectiveDAO
				.selectByExample(careerObjectiveExample);
		if (careerObjectiveList.isEmpty()) {
			careerObjectiveList.add(new TbCareerObjective());
		}
		TbCareerObjective currentCaeerObjective = careerObjectiveList.get(0);
		fullResume.setTbCareerObjective(currentCaeerObjective);

		// 查询自我评价
		fullResume.setSelfAppraise(currentCaeerObjective.getSelfAppraise());

		// 查询教育经历
		TbEducationExample educationExample = new TbEducationExample();
		educationExample.createCriteria().andResumeIdEqualTo(resumeId);
		List<TbEducation> educationList = this.educationDAO
				.selectByExample(educationExample);
		fullResume.setTbEducation(educationList);

		// 查询工作经验
		TbWorkExperienceExample workExperienceExample = new TbWorkExperienceExample();
		workExperienceExample.createCriteria().andResumeIdEqualTo(resumeId);
		List<TbWorkExperience> workExperiencesList = this.workExperienceDAO
				.selectByExample(workExperienceExample);
		fullResume.setTbWorkExperience(workExperiencesList);

		// 查询项目经验
		TbProjectExperienceExample projectExperienceExample = new TbProjectExperienceExample();
		projectExperienceExample.createCriteria().andResumeIdEqualTo(resumeId);
		List<TbProjectExperience> projectExperiencesList = this.projectExperienceDAO
				.selectByExample(projectExperienceExample);
		fullResume.setTbProjectExperience(projectExperiencesList);

		// 查询语言能力
		TbLanguageExample languageExample = new TbLanguageExample();
		languageExample.createCriteria().andResumeIdEqualTo(resumeId);
		List<TbLanguage> languagesList = this.languageDAO
				.selectByExample(languageExample);
		fullResume.setTbLanguage(languagesList);

		// 查询IT技能
		TbItskillExample itskillExample = new TbItskillExample();
		itskillExample.createCriteria().andResumeIdEqualTo(resumeId);
		List<TbItskill> itskillsList = this.itskillDAO
				.selectByExample(itskillExample);
		fullResume.setTbItskill(itskillsList);

		return fullResume;
	}

	public TbResume existsResume(TbResume resume) {
		TbResumeExample resumeExample = new TbResumeExample();
		// 必须添加用户Id和简历Id约束，否则将存在严重注入BUG
		resumeExample.createCriteria().andIdEqualTo(resume.getId())
				.andUserIdEqualTo(resume.getUserId());
		List<TbResume> resumeList = this.resumeDAO
				.selectByExample(resumeExample);
		if (resumeList.isEmpty()) {
			throw new BaseException(ExceptionKey.RESUME_NOT_FOUND);
		}
		return resumeList.get(0);
	}

	/**
	 * 保存标准简历
	 */
	public FullResume saveStandardResume(FullResume fullResume) {
		// 基本信息存在，做更新基本信息
		TbBasic basic = queryBasic(fullResume.getTbBasic());
		// 如果为空，保存基本信息
		if (basic == null) {
			fullResume.getTbBasic().setLanguageCode("zh-cn");
			this.basicDAO.insert(fullResume.getTbBasic());
		} else {
			fullResume.getTbBasic().setId(basic.getId());
			this.basicDAO.updateByPrimaryKeySelective(fullResume.getTbBasic());
		}
		Integer userId = fullResume.getTbBasic().getUserId();
		TbResume saveResume = fullResume.getTbResume();
		// 保存简历组
		TbPersonalResumeCategory savePersonalResumeCategory = new TbPersonalResumeCategory();
		savePersonalResumeCategory.setUserId(userId);
		savePersonalResumeCategory.setNAME(saveResume.getNAME());
		savePersonalResumeCategory.setPublishTypeCode(saveResume
				.getPublishTypeCode());
		this.personalResumeCategoryDAO.insert(savePersonalResumeCategory);
		Integer savePersonalResumeCategoryId = this.getTbDataBaseParamDAO()
				.selectLastInsertId();

		// 如果新建简历为默认简历，设置该用户的其他简历为非默认
		if (saveResume.getIsDefault()) {
			TbResumeExample updateResumeExample = new TbResumeExample();
			updateResumeExample.createCriteria().andUserIdEqualTo(userId);
			TbResume updateResume = new TbResume();
			updateResume.setIsDefault(false);
			this.resumeDAO.updateByExampleSelective(updateResume,
					updateResumeExample);
		}
		// 保存简历
		saveResume.setCategoryId(savePersonalResumeCategoryId);
		saveResume.setUserId(userId);
		saveResume.setIsBasic(true);
		saveResume.setIsEducation(true);
		saveResume.setIsCareerObjective(true);
		saveResume.setIsWorkExperience(true);
		saveResume.setIsTraining(false);
		saveResume.setIsProjectExperience(false);
		saveResume.setIsLanguage(false);
		saveResume.setIsITSkill(false);
		saveResume.setIsOther(false);
		saveResume.setIsEvaluate(false);
		saveResume.setLanguageCode("zh-cn");
		saveResume.setUpdateTime(new Date());
		saveResume.setCreateTime(new Date());
		this.resumeDAO.insert(saveResume);
		int saveResumeId = this.getTbDataBaseParamDAO().selectLastInsertId();

		// 保存教育经历
		List<TbEducation> saveEductionList = fullResume.getTbEducation();
		for (TbEducation tbEducation : saveEductionList) {
			tbEducation.setResumeId(saveResumeId);
			this.educationDAO.insert(tbEducation);
		}
		// 保存工作经验
		List<TbWorkExperience> saveWorkExperienceList = fullResume
				.getTbWorkExperience();
		for (TbWorkExperience tbWorkExperience : saveWorkExperienceList) {
			tbWorkExperience.setResumeId(saveResumeId);
			this.workExperienceDAO.insert(tbWorkExperience);
		}
		// 保存求职意向
		TbCareerObjective saveCareerObjective = fullResume
				.getTbCareerObjective();
		saveCareerObjective.setSelfAppraise(fullResume.getSelfAppraise());
		saveCareerObjective.setResumeId(saveResumeId);
		this.careerObjectiveDAO.insert(saveCareerObjective);
		
		//BasicSystem.User表更新
		User basicUser=new User();
		basicUser.setId(userId);
		basicUser.setEmail(fullResume.getTbBasic().getEmail());
		this.userDAO.updateByPrimaryKeySelective(basicUser);
		//JobWeb.User表更新
		this.tbUsersDAO.updateByPrimaryKeySelective(new TbUserAdapter(basicUser));
		return fullResume;
	}

	/**
	 * 查询基础信息
	 */
	public TbBasic queryBasic(TbBasic basic) {
		TbBasicExample basicExample = new TbBasicExample();
		TbBasicExample.Criteria cri = basicExample.createCriteria()
				.andUserIdEqualTo(basic.getUserId());
		if (basic.getId() != null) {
			cri.andIdEqualTo(basic.getId());
		}
		List<TbBasic> basicList = this.basicDAO.selectByExample(basicExample);
		if (!basicList.isEmpty()) {
			return basicList.get(0);
		}
		return null;
	}

	/**
	 * 写简历
	 */
	@Override
	public FullResume write(FullResume fullResume) {
		int userId = fullResume.getTbBasic().getUserId();
		TbResumeExample resumeExample = new TbResumeExample();
		resumeExample.createCriteria().andUserIdEqualTo(userId);
		int resumeCount = this.resumeDAO.countByExample(resumeExample);
		//限制创建简历个数
		int allowWriteResumeCount=getSystemMessagesInt(SystemKey.ALLOW_WRITE_RESUME_COUNT);
		if(resumeCount>=allowWriteResumeCount){
			throw new BaseException(ExceptionKey.RESUME_TOO_MORE,allowWriteResumeCount);
		}
		String email=fullResume.getTbBasic().getEmail();
		fullResume.setTbBasic(queryBasic(fullResume.getTbBasic()));
		TbBasic queryBasic=fullResume.getTbBasic();
		if(queryBasic==null)
		{
			queryBasic=new TbBasic();
			fullResume.setTbBasic(queryBasic);
		}
		queryBasic.setEmail(email);
		fullResume.initTbEductionList(1);
		fullResume.initTbWorkExperienceList(1);
		return fullResume;
	}

}
